package com.teaclipse.presentation;
import java.util.HashMap;
import java.util.ArrayList;

import com.teaclipse.model.TeaQuadGenerator;

/**
 * Manages the IManagedWindow interface that it is associated with
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class TeaWindowManager {

    /**
     * The reference to the managed window
     */
    private static IManagedWindow theManagedWindow = null;

    /**
     * A hash of windows IDs to window
     */
    private static HashMap windowHash = new HashMap();

    /**
     * a hash of window IDs to the contents of those windows
     */
    private static HashMap contentsHash = new HashMap();

    /**
     * The ID of the current window in focus
     */
    private static String currentWindow = "";

    /**
     * Used to ID new windows
     */
    private static int newDocumentNumber = 1;

    /**
     * List of window IDs to windows that have been saved
     */
    private static ArrayList unsavedList = new ArrayList();

    /**
     * Registers the managed window
     * @param managedWindow IManagedWindow
     */
    public static void registerManagedWindow(IManagedWindow managedWindow) {
        theManagedWindow = managedWindow;
    }

    public static IManagedWindow getManagedWindow()
    {
        return theManagedWindow;
    }

    /**
     * Creates a new document
     */
    public static void newDocument() {
        createNewDocument("document " + newDocumentNumber++);
    }

    /**
     * Creates a new document using the given document name
     * @param documentName String
     * @return String
     */
    protected static String createNewDocument(String documentName) {
        String windowID = generateWindowID();
        Object newWindow = theManagedWindow.createWindow(documentName, windowID);
        windowHash.put(windowID, newWindow);
        currentWindow = windowID;
        unsavedList.add(windowID);
        return windowID;
    }

    /**
     * Returns true is the current window is new
     * @return boolean
     */
    protected static boolean isCurrentWindowNew() {
        return unsavedList.contains(currentWindow);
    }

    /**
     * generates a window ID
     * @return String
     */
    protected static String generateWindowID() {
        return "WINDOW_" + System.currentTimeMillis();
    }

    /**
     * Opens a new window using the document name
     * @param documentName Object
     */
    public static void open(Object documentName) {
        String windowId = createNewDocument(documentName.toString());
        Object window = windowHash.get(windowId);
        contentsHash.put(windowId, documentName);
        theManagedWindow.fillContents(window, documentName);
        unsavedList.remove(windowId);
    }

    /**
     * Swithces the current window
     * @param windowId String
     */
    public static void switchDocument(String windowId) {
        Object window = windowHash.get(windowId);

        if (window != null) {
            currentWindow = windowId;
            theManagedWindow.switchWindow(window);
        } else {
            //System.out.println("Could not find window for windowID " + windowId);
        }

    }

    /**
     * Executes a save on the contents of the current window
     */
    public static void save() {

        Object window = windowHash.get(currentWindow);
        Object contents = contentsHash.get(currentWindow);
        theManagedWindow.writeContents(window, contents);
        unsavedList.remove(currentWindow);

    }

    /**
     * Executes a save as on the current widow content
     * @param documentName String
     */
    public static void saveAs(String documentName) {
        Object window = windowHash.get(currentWindow);
        theManagedWindow.writeContents(window, documentName);
        theManagedWindow.deleteWindow(window, currentWindow);
        createNewDocument(documentName);
        contentsHash.put(currentWindow, documentName);
        theManagedWindow.fillContents(windowHash.get(currentWindow),
                                      documentName);
        unsavedList.remove(currentWindow);
    }

    /**
     * Closes the window
     */
    public static void close()
    {
       if(windowHash.size() > 0)
       {
           unsavedList.remove(currentWindow);
           theManagedWindow.deleteWindow(windowHash.get(currentWindow),
                                         currentWindow);

         //  windowHash.remove(currentWindow);
       }

    }

    /**
     * Compiles the window
     */
    public static void compile() {

        String value = (String) contentsHash.get(currentWindow);
        TeaQuadGenerator generator = new TeaQuadGenerator(value);
    }
}
